<?php
// 导游控制器
class Controller_WWW_Guider extends Controller_WWW_Template
{
    public $file_path = "";
    public $identity_pic_path = "";
    public $path = "";
    public function __construct ($req, $res)
    {
        $this->file_path = "upload/icon/";//头像存放目录
        $this->identity_pic_path = "upload/icon/guider/identity_pic/";//认证图片存放目录
        $this->path="upload/tourist_pic/";//评论图片存放目录
        $this->template = "smarty:www/guider/index";
        parent::__construct($req, $res);

    }

    public function before ()
    {
        parent::before();

        if ($this->_uid == 0)
        {
            if($this->request->action() == 'confirm'){
                return ;
            }
            $this->request->redirect("/");
        }
        $user = $this->_user->getUserInfo();
        $type = Arr::get($user,'userType');
        if($type != 'GUIDER'){
            $this->request->redirect("/");
        }
    }

    public function action_index ()
    {
        //
        $this->action_info();
    }

    // 我的路线
    public function action_track_list ($page = 1)
    {
        $page   = max($page, 1);
        $limit  = 10;
        $offset = ($page - 1) * $limit;

        $track  = new App_Track;
        list($counts, $trackList) = $track->search($this->_uid, $offset, $limit);
        $pagination = $this->pagination($counts, $limit, $page_type = 'route', $page_key = 'param1', $views = 'www/page/new');
        $this->template->set('pagination', $pagination);
        $this->template->set('countsTrack', $counts);
        $this->template->set('trackList', $trackList);
        $this->template->set('step', $this->request->action());
    }

    public function action_activity_list ($page = 1)
    {
        $page   = max($page, 1);
        $limit  = 10;
        $offset = ($page - 1) * $limit;

        $track  = new App_Activity();
        list($counts, $trackList) = $track->search($this->_uid, $offset, $limit);
        $pagination = $this->pagination($counts, $limit, $page_type = 'route', $page_key = 'param1', $views = 'www/page/new');
        $this->template->set('pagination', $pagination);
        $this->template->set('countsTrack', $counts);
        $this->template->set('trackList', $trackList);
        $this->template->set('step', $this->request->action());
    }

    // 预定记录之订单管理入口
    public function action_orders_paid ($page = 1)
    {
        $page   = max($page, 1);
        $limit  = 5;
        $offset = ($page - 1) * $limit;

        $order  = new Order($this->_user);
        list($count, $orders) = $order->listOrdersPaid($offset, $limit);

        $pagination = $this->pagination($count, $limit, $page_type = 'route', $page_key = 'param1', $views = 'www/page/new');
        $this->template->set('pagination',$pagination);
        $this->template->set('countsOrders', $count);
        $this->template->set('orders', $orders);
        $this->template->set('step', $this->request->action());
    }

    // 订单管理之订单详情(导游的订单)
    public function action_order_detail ($orderNo)
    {
        $orderDetail = Order::detail($orderNo);
        $isOrdersofGuider=0;
        // 不是自己导游的订单，或者不是自己下的订单，认为无权访问
        if(empty($orderDetail) || 
            ($orderDetail['shopId'] != $this->_uid && $orderDetail['uid'] != $this->_uid))
        {
            $this->request->redirect('/');
            return;
        }
        if($orderDetail['shopId'] != $this->_uid && $orderDetail['uid'] == $this->_uid)
        {
            $isOrdersofGuider=1;
        }

        $this->template->set('title', '订单详细');
        $this->template->set('order', $orderDetail);
        $this->template->set('isOrdersofGuider', $isOrdersofGuider);
        $this->template->set('step', $this->request->action());

        // 订单日志
        $log  = new Order_Log(Arr::get($orderDetail, 'id'));
        $logs = $log->getLogs();
        $this->template->set('logs', $logs);
    }

    // 预定记录之预定申请入口
    public function action_orders_request ($page = 1)
    {
        $page   = max($page, 1);
        $limit  = 5;
        $offset = ($page - 1) * $limit;

        $order  = new Order($this->_user);
        list($count, $orders) = $order->listOrdersRequest($offset, $limit);
        $pagination = $this->pagination($count, $limit, $page_type = 'route', $page_key = 'param1', $views = 'www/page/new');
        $this->template->set('pagination', $pagination);
        $this->template->set('countsOrders', $count);
        $this->template->set('orders', $orders);
        $this->template->set('step', $this->request->action());
    }

    // 玩伴自己下的订单(活动/路线订单列表)
    public function action_my_orders ( $page = 1)
    {
        $page   = max($page, 1);
        $limit  = 10;
        $offset = ($page - 1) * $limit;

        $order  = new Order($this->_user);
        list($count, $orders) = $order->listOrdersByTourist( $offset, $limit);
        $pagination = $this->pagination($count, $limit, $page_type = 'route', $page_key = 'param1', $views = 'www/page/new');
        $this->template->set('pagination', $pagination);
        $this->template->set('countsOrders', $count);
        $this->template->set('orders', $orders);
        $this->template->set('step', $this->request->action(). '_track' );
    }
    
     //展示路线、活动、自营评论
    public function action_line_comments($page = 1)
    {
        $page   = max($page, 1);
        $limit  = 5;
        $offset = ($page - 1) * $limit;

        list($count,$list) = User_TouristComments::get_track($this->_user,$offset,$limit);
        $pagination = $this->pagination($count, $limit, $page_type = 'route', $page_key = 'param1', $views = 'www/page/new');
        $this->template->set('pagination', $pagination);

        $this->template->set("count",$count);
        $this->template->set("list",$list);
        $this->template->set("step","line_comments");
    }
    //添加路线、活动、自营评论
    public function action_add_line_comment($type,$oid)
    {
        $post = $this->request->post();
        if(empty($post)){
            $data = User_TouristComments::tourist_track_check_out('oid',$oid)->current();
            if(!empty($data)){
                $this->request->redirect('/guider/show_line_comment/'.$data['id']);
            }
            $orderDetail = Order::detail($oid);
            $this->template->set("step","add_line_comment");
            $this->template->set("data",$orderDetail);
        }else{
            list($file,$flag) = User_TouristComments::add_resource($_FILES['tourist_pic'],$this->path);
            if($flag){
                $data['tourist_pic'] = $file;
                $data['uid'] = Arr::get($post,'uid');
                $data['track_id'] = Arr::get($post,'track_id');
                $data['track_name'] = Arr::get($post,'track_name');
                $data['guider_id'] = Arr::get($post,'guider_id');
                $data['track_score'] = Arr::get($post,'track_score');
                $data['persons'] = Arr::get($post,'persons');

                $data['guider_score'] = Arr::get($post,'guider_score');
                $data['comments'] = htmlspecialchars($post['comments']) ;
                $data['created'] = time() ;
                $data['oid'] = Arr::get($post,'id') ;
                $type=trim($type);
                $type=strtoupper($type);
                $data['type'] = $type ;

                User_TouristComments::tourist_track_add($data);
                
                $where = array(
                         'key'=>'type',
                         'cond'=>'=',
                         'val'=> $type
                         );
                //更改订单状态为已评价
                User_TouristComments::updateOrderStatus($data['oid']);

                $avg = User_TouristComments::tourist_track_score('track_id',$data['track_id'],$where);
                $save['score'] = $avg*100;
                $log = new Order_Log($oid);
                $log->add("游客进行了评价");
                switch ($type){
                      case 'TRACK':
                            User_TouristComments::update_score($save,'mod_track','id',$data['track_id']);
                            break;
                      case 'ACTIVITY':
                            User_TouristComments::update_score($save,'mod_activity','id',$data['track_id']);
                            break;
                      default:
                            User_TouristComments::update_score($save,'mod_ziying','id',$data['track_id']);
                            break;
                }

                $this->request->redirect("/guider/line_comments");
            }
         }
    }

    //展示路线、活动、自营评论详细
    public function action_show_line_comment($id)
    {
        $data = User_TouristComments::tourist_track_check_out('id',$id)->current();
        if(!empty($data['tourist_pic'])){
        $tourist_pic = explode(';',trim($data['tourist_pic'],';'));
        $this->template->set('tourist_pic', $tourist_pic);
        }
        $this->template->set('data', $data);
        $this->template->set('step', "show_line_comment");
    }

    // 预订记录之处理预订申请
    public function action_order_handle_request ($orderNo)
    {
        $post = $this->request->post();
        // 处理审批结果
        if (! empty($post))
        {
            $action  = Arr::get($post, 'action');
            $message = Arr::get($post, 'message');

            $order = new Order;
            $order->verifyRequest($orderNo, $action, $message);

            $ret = array('status' => 0, 'message' => '');
            echo json_encode($ret); exit;
        }
        else
        {
	        $orderDetail = Order::detail($orderNo);

	        if(empty($orderDetail) || $orderDetail['shopId'] != $this->_uid )
	        {
	            $this->request->redirect('/');
	            return;
	        }

            $this->template->set('step', $this->request->action());
	        $this->template->set('title', '预订审批');
	        $this->template->set('order', $orderDetail);
        }
    }

    // 导游中心之我的收款账户
    public function action_bank_list ()
    {
        $this->template->set('step', $this->request->action());
	    $this->template->set('title', '我的收款账户');

        $bank  = new UserExt_Bank($this->_uid);
        $banks = $bank->getBankList();
        foreach ($banks as & $b){
            if(strlen($b['accountUserName'])>15){
            	$b['accountUserName']=utf8::substr($b['accountUserName'],0,15)."...";
            }
        }
        $this->template->set('bankList', $banks);
    }

    // 导游中心之新增收款账户
    public function action_bank_create ()
    {
        $post = $this->_post;
        if (empty($post))
        {
            $this->template->set('step', $this->request->action());
	        $this->template->set('title', '新增收款账户');
        }
        else
        {
            $type     = trim(Arr::get($post, 'accountType'));
            $name     = trim(Arr::get($post, 'accountName'));
            $userName = trim(Arr::get($post, 'accountUserName'));

            $bank  = new UserExt_Bank($this->_uid);
            $bank->addBank($type, $name, $userName);

            $this->request->redirect('/guider/bank_list');
        }
    }



    // 导游中心：奖励入口
    public function action_prize ()
    {
        $post = $this->request->post();
        if (empty($post))
        {
            $popu = new Popularize;
            $this->template->set('step', 'prize');
            $this->template->set('prizeMoney', $popu->getInviteMoney());
            $this->template->set('myInviteCode', $popu->getInviteCode($this->_uid));
            $this->template->set('hasRequestInvitePrize', $popu->userHasRequestInvitePrize($this->_uid));
        }
        else
        {
            $code = Arr::get($post, 'code');
            $ret = array('status' => 0, 'message' => '');

            if (empty($code))
            {
                $ret['status']  = 1;
                $ret['message'] = '邀请码不能为空';
            }
            else
            {
                $popu = new Popularize;
                try {
                    $popu->addInvite($this->_uid, $code);
                    $ret['message'] = '提交成功，请等待审批！';
                }
                catch (Exception $e)
                {
                    $ret['status']  = 1;
                    $ret['message'] = $e->getMessage();
                }
            }

            echo json_encode($ret);exit;
        }
    }

    // AJAX 申请奖励
    public function action_requestPrize ($taskType, $taskId)
    {
        $populize = new Popularize;
        if ($populize->userHasRequestFirstSubmittedPrize($this->_uid))
        {
            $ret = array('status' => 0, 'message' => '已经申请过了');
        }
        else
        {
            $populize->addRequest($taskType, $taskId, $this->_uid);
            $ret = array('status' => 0, 'message' => '申请成功');
        }

        echo json_encode($ret);
        exit;
    }

    public function action_info(){
        $user = user::currentUser();
        $uid = $user->getUserUid();

        $user_info = User_UserInfo::user_info($uid)->current();
        if(empty($user_info)){
         // $this->request->redirect("/guider/add_guider_info/$uid");
          $this->request->redirect("/guider/update_guider_info/$uid");
        }
        $this->rebuild_list($user_info);
        $this->template->set("user",$user_info);
        $this->template->set("step","user_info");

    }


    // 添加用户信息
    public function action_add_guider_info($uid){
        $user_info = User_UserInfo::user_info($uid)->current();
        if(!empty($user_info)){
            $this->request->redirect("/tourist/info");
        }
        $post = $this->request->post();

        if(!empty($post) && !empty($_FILES)){
            $data1 = array('cn'=>0,'fr'=>'0','en'=>'0');
            foreach($post['language'] as $v){
                if($v == 1){
                    $data1['cn'] = 1;
                }else if($v == 2){
                    $data1['fr'] = 1;
                }else if($v == 3){
                    $data1['en'] = 1;
                }
            }
            $data1['userName'] = $post['userFamily'].$post['userName'];
            User_Account::update($data1,'uid',$uid);
            $post['language'] = implode(',',$post['language']);

            list($mag,$status) = User_UserInfo::add_resource($_FILES['userIcon'],$this->file_path);
            if($status){
                $post['userIcon'] = $mag;
                $data['userIcon'] = $mag;
                User_Account::update($data,'uid',$uid);
                if(User_UserInfo::create($post)){
                    $this->request->redirect("/guider/info");
                }
            }else{
                echo $mag;
                exit;
            }
        }else{
            $this->template->set("uid",$uid);
            $this->template->set("step","add_info");
        }
    }

    public function action_update_guider_info($uid=""){
        $uid = $uid == "" ? $this->_uid : $uid;
        $post = $this->request->post();

        if(!empty($post)){
            $data1 = array('cn'=>0,'fr'=>'0','en'=>'0');
            foreach($post['language'] as $v){
                if($v == 1){
                    $data1['cn'] = 1;
                }else if($v == 2){
                    $data1['fr'] = 1;
                }else if($v == 3){
                    $data1['en'] = 1;
                }
            }

            $post['language'] = implode(',',$post['language']);
            unset($post['uid']);
            //$post['userPhone'] = $post['telcode'].$post['userPhone'];
            //unset($post['telcode']);
            $row = User_UserInfo::update($post,'uid',$uid);
            $data1['userName'] = $post['userFamily'].$post['userName'];
            $data1['userGender'] = $post['userGender'] ;
            User_Account::update($data1,'uid',$uid);
            if($_FILES['userIcon']['name'] != ''){
                list($mag,$status) = User_UserInfo::add_resource($_FILES['userIcon'],$this->file_path);
                if($status){
                    $data['userIcon'] = $mag;
                    User_UserInfo::update($data,'uid',$uid);
                    User_Account::update($data,'uid',$uid);
                }
            }
            $this->request->redirect("/guider/info");
        }else{
            $this->template->set("uid",$uid);
            $user_info = User_UserInfo::user_info($uid)->current();
            $user_info['language'] = explode(',', Arr::get($user_info, 'language'));
            $user_info['telCode']=substr($user_info['userPhone'],0,3);
            $user_info['userPhone']=substr($user_info['userPhone'],3);
            $this->template->set("user",$user_info);
            //$this->template->set("step","edit_info");
            $this->template->set("step","add_info");
        }
    }


    public function action_identity(){
        $post = $this->request->post();
        if(empty($post)){
            $user = user::currentUser();
            $uid = $user->getUserUid();
            $user_info = User_Account::user_account_info($uid)->current();
            $this->template->set("user",$user_info);
            $this->template->set("step","identity_credit");
        }else{
            list($msg,$status) = User_UserInfo::add_resource_pic($_FILES['identityPicture'],$this->identity_pic_path);
            list($msg1,$status1) = User_UserInfo::add_resource_pic($_FILES['identityPicture2'],$this->identity_pic_path);
            if($status || $status1){
//                $post['identityPicture'] = $msg;
                if($status1){
                    $save['identityPicture'] = $msg.';'.$msg1;
                }else{
                    $save['identityPicture'] = $msg;
                }

                $save['identityErea'] = Arr::get($post,'identityErea');
                if(empty($save['identityErea'])){
                    $save['identityErea'] = Arr::get($post,'major')."-|-".Arr::get($post,'school');
                }
                $save['identityName'] = $post['identityName'];
                $save['identityActive'] = 0;
                $save['changed'] = time();
                User_Account::update($save, 'uid', $this->_uid);
            }
            $this->request->redirect('/guider/show_identity');
        }
    }
        
    public function action_show_identity(){    
        $user = user::currentUser();
        $uid = $user->getUserUid();
        $user_info = User_Account::user_account_info($uid)->current();
        if($user_info['identityErea'] == ""){
            $this->request->redirect("/guider/identity");
        }
        if($user_info['identityName'] == '留学生'){
            $data = explode('-|-',$user_info['identityErea']);
            $user_info['identityErea'] = Arr::get($data,0).'-'.Arr::get($data,1);
        }
        $this->template->set("step","show_credit");
        $this->template->set("user",$user_info);
    }


    public function action_phone_credit(){
        $post = $this->request->post();
        if(empty($post)){
            $user = user::currentUser();
            $uid = $user->getUserUid();
            $user_info = User_Account::user_account_info($uid)->current();
            $data = $this->phone_area_code();
            $this->template->set("user",$user_info);
            $this->template->set("data",$data);
            $this->template->set("uid",$uid);
            $this->template->set("step","phone_credit");
        }else{
            $areacode = Arr::get($post,'areacode'); //国际区号
            $num = trim(Arr::get($post,'num'));           //电话号
            $code = Arr::get($post,'code');         //验证码
            $uid = Arr::get($post,'uid');           //用户id
            list($status,$msg) = $this->get_message($code,$areacode.$num);
            if($status){
                $data['telActive'] = 1;
                $data['telCode'] = $areacode;
                $data['telNum'] = $num;
                $data['changed'] = time();
                User_Account::update($data, 'uid', $uid);
                $data1['userPhone']= "+".$areacode.$num;
                User_UserInfo::update($data1, 'uid', $uid);
                $this->template->set("step",'phone_success');
                $this->template->set("num",$num);
                $this->template->set("code",$code);
                $datas = $this->phone_area_code($areacode);;
                $this->template->set("areacode",$areacode);
                $this->template->set("country",Arr::get($datas,'0'));
//                $this->request->redirect("/guider/show_phone_credit");
            }else{
                echo $msg;
                exit;
            }

        }
    }

    public function action_valid_phone(){
    	$post = $this->request->post();
    	$num= Arr::get($post, 'num');
    	$phoneNum= Arr::get($post, 'phoneNum');
    	$count = DB::select()
    	->from('user_v2')
    	->where('telNum','=',$num)
    	->execute()
    	->count();
    	$ret=array('status'=>0,'msg'=>null,'phoneNum'=>$phoneNum);
    	if($count==0){
    		$ret['msg']='电话可以使用';
    	}else{
    		$ret['msg']='电话已经存在';
    		$ret['status']=1;
    	}
    	echo json_encode($ret);
    	exit;
    }
    public function action_show_phone_credit(){
        $user = user::currentUser();
        $uid = $user->getUserUid();
        $user_info = User_Account::user_account_info($uid)->current();
        if($user_info['telCode'] == ""){
            $this->request->redirect("/guider/phone_credit");
        }
        $code_info = $this->phone_area_code($user_info['telCode']);
        $this->template->set("step","show_phone_credit");
        $this->template->set("user",$user_info);
        $this->template->set("code_info",$code_info[0]);
    }

    public function action_send_msg($num){
        $sms = new Util_YunPianSms;
        $code = mt_rand(0,9).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9);
        $data['mobile'] = $num;
        $data['code'] = $code;
        $data['created'] = time();
        $id = $this->save($data);
        if($id > 0){
            $tip = array('status'=>1,'msg'=>'fail');
            $text="【WAYAtrip】Your verification code is $code";
            $rs = $sms->send_sms_to_user($text,$num);
            $rs = json_decode($rs,true);
            if($rs['code'] == 0){
                $tip = array('status'=>0,'msg'=>'success');
                echo json_encode($tip);
                exit;
            }
//            var_dump($rs);
//            var_dump($num,$rs);
        }else{
            $tip = array('status'=>1,'msg'=>'fail');
        }
        echo json_encode($tip);
        exit;

    }
    //玩伴收到的评论的列表
    public function action_recieved_comments($page = 1){
        $user = user::currentUser();
        $uid = $user->getUserUid();
        $page     = max(1, $page);
        $limit    = 10;
        $offset   = $limit * ($page - 1);
        list($counts,$list) = Util_Track::getTrackList($limit,$offset,$uid);
        
        $pagination = $this->pagination($counts, $limit, $page_type = 'route', $page_key = 'param1', $views = 'www/page/new');
        $this->template->set('pagination', $pagination);

        $this->template->set('list', $list);
        $this->template->set('step', 'line_comment_list');
    }
    //展示收到的评论的详情
    public function action_show_recieved_comment($id){
        $user = user::currentUser();
        $uid = $user->getUserUid();
        $query = $this->request->query();
        $type= Arr::get($query, 'type');
        $detail = Util_Track::getTrackDetail($uid,$id,$type);

        $this->template->set('data', $detail);
        $this->template->set('step', 'show_line_comment');
    }

    public function action_show_activity_comments($id){
        $user = user::currentUser();
        $uid = $user->getUserUid();
        $detail = Util_Activity::getActivityDetail($uid,$id);
//        var_dump($detail);
        $this->template->set('data', $detail);
        $this->template->set('step', 'show_activity_comment');
    }


    public function action_message($page = 1){
        $user = user::currentUser();
        $uid = $user->getUserUid();
        $page   = max($page, 1);
        $limit  = 5;
        $offset = ($page - 1) * $limit;
        list($count,$list) = User_Chat::getGuiderChatByUid($uid,$limit,$offset);
        $pagination = $this->pagination($count, $limit, $page_type = 'route', $page_key = 'param1', $views = 'www/page/new');
        $this->template->set("pagination",$pagination);
        $this->template->set("list",$list);
//        var_dump($list);

        $this->template->set("step",'message_list');
    }



    public function action_credit(){
        $user = user::currentUser();
        $uid = $user->getUserUid();
        $user_info = User_Account::user_account_info($uid)->current();
        $this->template->set("user",$user_info);
        $this->template->set("step","credit");
    }

    public function action_subscribe(){
//        $this->template->set("step","credit");
        $this->template->set("step","subscribe");
        $post = $this->request->post();
        if(!empty($post)){
            $data = User_Subscribe::user_activation($post['uid'],$post['email'],'guider');
//            var_dump($data);
            $this->template->set("data",$data);
            if(!$data['status']){
                echo "发送失败";
                exit;
            }
        }
    }

    //用户修改认证邮箱
    public function action_update_mail($uid){
        $post = $this->request->post();
        if(!empty($post)){
            User_Subscribe::user_activation($post['uid'],$post['email']);
        }else{
            $this->template->set("uid",$uid);
            $this->template->set("step","subscribe_update");
        }

    }
    //验证邮箱是否存在
    public function action_valid_mail(){
    	$post = $this->request->post();
    	$email= Arr::get($post, 'email');
    	$count = DB::select()
    	->from('user_v2')
    	->where('userId','=',$email)
    	->execute()
    	->count();
    	$ret=array('status'=>0,'msg'=>null);
    	if($count==0){
    		$ret['msg']='邮箱可以使用';
    	}else{
    		$ret['msg']='邮箱已经存在';
    		$ret['status']=1;
    	}
    	echo json_encode($ret);
    	exit;
    	 
    }
    //验证用户邮件
    public function action_confirm(){
        if($this->_uid > 0){
            $query = $this->request->query();
            $token = Arr::get($query,'token');
            list($status,$msg) = User_Subscribe::confirm_token($token);
            $this->template->set("data",$msg);
            $this->template->set("step","confirm");
            if($status){
                $this->template->set("confirm",$msg);
            }else{

            }
        }else{
            $this->template->set_filename('smarty:www/guider/valid_fail');
        };

    }

    public function action_verify(){
        $query = $this->request->query();
        $code = Arr::get($query,"code");
        $num = Arr::get($query,"num");
        list($status,$msg) = $this->get_message($code,$num);
        if($status){
            $ret = array('status'=>0,'msg'=>$msg);
        }else{
            $ret = array('status'=>1,'msg'=>$msg);
        }
        echo json_encode($ret);
        exit;

    }

    public function action_message_remove($id){
        $data = User_Chat::removeGuiderChatByUid($id);
        if($data > 0){
            $this->request->redirect("guider/message");
        }
    }


    public function rebuild_list(&$data){
        $data['language'] = explode(',', Arr::get($data, 'language'));
//        $tmp = "";
//        foreach($data['language'] as $v){
//           if($v == '1'){
//               $tmp .= "中文,";
//           }elseif($v == '2'){
//               $tmp .= "法文,";
//           }elseif($v == '3'){
//               $tmp .= "英文,";
//           }
//        }
//
//        $data['language'] = trim($tmp,",");
//        unset($tmp);
    }

    public function phone_area_code($code = ""){
        $data = DB::select()->from("phone_area_code");
        if($code != ""){
            $data = $data->where("areaCode",'=',$code);
        }

        $data =  $data->execute()->as_array();
        return $data;
    }

    public function save($data){
        list($insert_id, $rows) = DB::insert('user_v2_mobile_code')
            ->columns (array_keys($data))
            ->values (array_values($data))
            ->execute();
        return $insert_id;
    }

    public function get_message($code,$num){
        $rs = DB::select()
            ->from('user_v2_mobile_code')
            ->where('code','=',$code)
            ->where('code','=',$code)
            ->where('mobile','=',"+".$num)
            ->order_by('created','desc')
            ->execute();
        if($rs->count() > 0){
            $arr = $rs->current();
            $sub = time() - $arr['created'];
            if($sub > 1200){
                return array(false,'验证码时间超时');
            }
            return array(true,'验证通过');
        }
        return array(false,'验证码不正确');
    }

    public function action_notice($page = 1){
        $page   = max($page, 1);
        $limit  = 10;
        $offset = ($page - 1) * $limit;
        list($counts,$notice) = Util_Notice::noticeList($limit,$offset);
        $pagination = $this->pagination($counts, $limit, $page_type = 'route', $page_key = 'param1', $views = 'www/page/new');
        $this->template->set('pagination', $pagination);
        $this->template->set("step","notice");
        $this->template->set("data",$notice);
    }

    public function action_show_notice($nid){
        $ret['status'] = 1;
        if(Util_Notice::noticeDetail($nid)){
            $ret['status'] = 0;
        } ;
        echo json_encode($ret);
        exit;
    }

    public function action_remove_notice($nid){
        $status = Util_Notice::remove($nid);
        if($status){
            $this->request->redirect('/guider/notice');
        }
    }

}
